package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysMenuService;
import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.config.exception.GuiguException;
import com.atguigu.common.jwt.JwtHelper;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.LoginVo;
import com.atguigu.vo.system.RouterVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.jsonwebtoken.Jwt;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author vvv
 * @date 2023-03-09 15 06
 * discription
 */
@Api(tags = "后台登录管理")
@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService sysMenuService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo) {
        //根据用户名查询数据库
        String username = loginVo.getUsername();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<SysUser>();
        queryWrapper.eq(SysUser::getUsername, username);
        SysUser sysUser = sysUserService.getOne(queryWrapper);

        if (sysUser == null) {
            throw new GuiguException(201, "用户不存在");
        }
        //判断密码
        String passwordDB = sysUser.getPassword();
        String passwordInput = MD5.encrypt(loginVo.getPassword());
        if (!passwordDB.equals(passwordInput)) {
            throw new GuiguException(201, "密码错误");
        }

        //用户是否被禁用 0 禁用 1 没禁用
        if (sysUser.getStatus().intValue() == 0) {
            throw new GuiguException(201, "用户已经被禁用请联系管理员");
        }
        //根据jwt生成token字符串
        String token = JwtHelper.createToken(sysUser.getId(), sysUser.getUsername());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        return Result.ok(map);
    }

    @GetMapping("/info")
    public Result info(HttpServletRequest request) {
        //获取请求头信息->token 字符串
        String token = request.getHeader("token");
        //从token中获取用户id
        Long userId = JwtHelper.getUserId(token);
        //根据id查询用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //根据用户id获取用户可以操作的菜单列表
        List<RouterVo> routerList = sysMenuService.findUserMenuListByUserId(userId);

        //根据用户id获取用户可以操作按钮的列表
        List<String> permsList = sysMenuService.findUserPermsByUserId(userId);


        Map<String, Object> map = new HashMap<>();
        map.put("roles","[admin]");
        map.put("name", sysUser.getName());
        map.put("avatar","https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
        map.put("routers", routerList);
        map.put("buttons", permsList);
        return Result.ok(map);
    }

    @PostMapping("logout")
    public Result logout(){
        return Result.ok();
    }
}
